using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mgh.Data.Sql;
using Jewelry.BussinessLayer;
using Jewelry.Entities;
using System.Globalization;
using Mgh.Data;

public partial class Pages_UShipment : Mgh.UI.Sql.PageBase
{
    #region properties

    protected CultureInfo myInfo;

    private DataSources.Jw_Tbl_ShipmentDataTable Shipment
    {
        get { return Session["shipment"] == null ? null : Session["shipment"] as DataSources.Jw_Tbl_ShipmentDataTable; }
        set { Session["shipment"] = value; }
    }

    private DataSources.Jw_Tbl_PaymentDataTable Payment
    {
        get { return Session["payment"] == null ? null : Session["payment"] as DataSources.Jw_Tbl_PaymentDataTable; }
        set { Session["payment"] = value; }
    }

    #endregion

    #region events

    override protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!CurrentUser.checkUser())
            Response.Redirect("ULogin.aspx?url=" + HttpUtility.UrlEncode(Request.Url.PathAndQuery));
        if (!IsPostBack)
        {
            if (Payment == null)
                Response.Redirect("UPayment.aspx");
            int cp = 1000000;
            Label1.Text = cp.ToString("$#,#", myInfo);
            bindData();
        }
    }

    protected void btSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Shipment = this.prepairData();
            if (Utility.tableNullOrEmpty(Payment) || Utility.tableNullOrEmpty(Shipment))
            {
                Response.Redirect("UPayment.aspx");
            }
            OrderBLL bn = new OrderBLL(getCurrentConnect());
            if (bn.finishOrder(CurrentUser.Username, Payment, Shipment))
            {
                Response.Redirect("UViewPayment.aspx");
            }
            else
            {
                cval.IsValid = false;
                cval.ErrorMessage = bn.getMessage();
            }
        }
    }

    #endregion

    #region functions

    private DataSources.Jw_Tbl_ShipmentDataTable prepairData()
    {
        DataSources.Jw_Tbl_ShipmentDataTable tb = new DataSources.Jw_Tbl_ShipmentDataTable();
        DataSources.Jw_Tbl_ShipmentRow row = tb.NewJw_Tbl_ShipmentRow();
        row.QuocGia = dlQuocGia.SelectedValue;
        row.HoTen = TextBox1.Text;
        row.DiaChi = TextBox2.Text;
        row.ThanhPho = TextBox3.Text;
        row.SoLienLac = TextBox4.Text;
        row.PhiVanChuyen = int.Parse(Label1.Text.Replace("$", "").Replace(",", ""));
        row.BatDau = System.DateTime.Now;
        row.TrangThai = "Waiting";
        row.OrID = string.Empty;
        tb.AddJw_Tbl_ShipmentRow(row);
        return tb;
    }

    private void bindData()
    {
        if (Shipment != null && Shipment.Count > 0)
        {
            dlQuocGia.SelectedValue = Shipment[0].IsQuocGiaNull() ? string.Empty : Shipment[0].QuocGia;
            TextBox1.Text = Shipment[0].IsHoTenNull() ? string.Empty : Shipment[0].HoTen;
            TextBox2.Text = Shipment[0].IsDiaChiNull() ? string.Empty : Shipment[0].DiaChi;
            TextBox3.Text = Shipment[0].IsThanhPhoNull() ? string.Empty : Shipment[0].ThanhPho;
            TextBox4.Text = Shipment[0].IsSoLienLacNull() ? string.Empty : Shipment[0].SoLienLac;
            Label1.Text = Shipment[0].IsPhiVanChuyenNull() ? "$0" : Shipment[0].PhiVanChuyen.ToString("$#,#", myInfo);
        }
    }

    #endregion
}
